package com.moleq.mgpos.widget;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import com.moleq.base.SmartBeep;
import com.moleq.mgpos.widget.NumberPad.NumberPadListener;
import com.moleq.mgpos.activities.R;
import com.moleq.util.MoleQUtil;

public class NumberPadForPayment extends LinearLayout {

	public final static int CHECK_OUT_STYLE = 2;
	public final static int LOGIN_STYLE = 0;
	public final static int SALE_STYLE = 1;
	public static final int NO_ENTER_STYLE = 3;
	public static final int NO_ENTER_DOT_STYLE = 4;

	boolean hasFinalResult = false;
	public boolean isReadOnly;
	private LayoutInflater mInflater;
	private Context mContext;
	private Button mButtonKey_0, mButtonKey00, mButtonKey01, mButtonKey02,
			mButtonKey03, mButtonKey04, mButtonKey05, mButtonKey06,
			mButtonKey07, mButtonKey08, mButtonKey09, //mButtonKeyX,
			mButtonKeyClear, mButtonKeyBack, mButtonKeyEnter;
	private EditText mNumberPadInput;
	private String mQty;
	private boolean isMultiply;

	private int mMaxLength = 15;
	//private String mInputBuffer;
	
	public void setmMaxLength(int mMaxLength) {
		this.mMaxLength = mMaxLength;
	}


	public NumberPadForPayment(Context context) {

		super(context);
		constructor(context);
	}

	public NumberPadForPayment(Context context, AttributeSet attrs) {

		super(context, attrs);
		constructor(context);
	}
	
	

	// Listener
	private OnClickListener mOnButtonClick2 = new OnClickListener(){

		@Override
		public void onClick(View v) {
			String strInput = mNumberPadInput.getText().toString().trim();
				
				String str = String.valueOf(((Button)v).getText());
				switch(v.getId()){
				case R.id.numberPadKey_0:
					//
					onZeroClick();
					break;
				case R.id.numberPadKey01:
					//
				case R.id.numberPadKey02:
					//
				case R.id.numberPadKey03:
					//
				case R.id.numberPadKey04:
					//
				case R.id.numberPadKey05:
					//
				case R.id.numberPadKey06:
					//
				case R.id.numberPadKey07:
					//
				case R.id.numberPadKey08:
					//
				case R.id.numberPadKey09:
					
					//onValueChanged(str);
					onNumberClick(str);
				
					break;
				case R.id.numberPadKey00:
					onDoubleZeroClick();
					break;
				case R.id.numberPadKeyX:
					onMultiplyClick();
					break;
				case R.id.numberPadKeyClear:
					onClearClick();
					break;
				case R.id.numberPadKeyBack:
					onBackClick();
					break;
				case R.id.numberPadKeyEnter:
					onEnterClick();
					break;
				case R.id.numberPadKeyDot:
					onDotClick();
					break;
				}
				
				if(mNumberPadListener!=null)
					mNumberPadListener.onClick((NumberPadButton)v.getTag());
				
				
				
//				mNumberPadInput.setSelection(strInput.length());
			
			
		}

		
		
	};
	
	private OnTouchListener editTextOnTouchEvent = new OnTouchListener(){

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			
			
			int inType = mNumberPadInput.getInputType();
			mNumberPadInput.setInputType(InputType.TYPE_NULL);
			mNumberPadInput.onTouchEvent(event);
			mNumberPadInput.setInputType(inType);
			mNumberPadInput.setSelection(mNumberPadInput.getText().length());
			return true;
			
		}
		
	};
	
	private void setText(String str){
		mNumberPadInput.setText(str);
		mNumberPadInput.setSelection(str.length());
	}
	
//	private void onValueChanged(String str) {
//		if(TextUtils.isEmpty(str)) return;
//		
//		String str1  = String.valueOf(this.mNumberPadInput.getText());
//		
//		for(String str2 = numBuilder(str,str1);;str2 = valueBuilder(str,str1)){
//			
//			this.mNumberPadInput.setText(str2);
//			
//		}
//		
//	}

	protected void onEnterClick() {
		
		
		//this.mNumberPadListener.onEnterClick();
		
	}

	protected void onBackClick() {
		String strInput = mNumberPadInput.getText().toString().trim();
		if (strInput.equals("")) return ;
			
		int len = strInput.length();
		if (len > 0) {
			int endIndex = len - 1;
			if (endIndex == 0) {
				strInput = "";
			} else {
				strInput = strInput.substring(0, endIndex);
			}
			
			this.setText(strInput);
			
		}
		
	}

	protected void onNumberClick(String pStr) {
//		String strInput = mNumberPadInput.getText().toString().trim();
//		if (strInput.length() < mMaxLength){
//			strInput+= pStr;
//			this.setText(strInput);
//		}
		
		String strInput = mNumberPadInput.getText().toString().trim();
		if (strInput.length() < mMaxLength){
				if (strInput.contains(".")) {
					int positionOfDot = strInput.indexOf(".");
					int iDigital = 0;
					iDigital = strInput.substring(positionOfDot).length() - 1;
					switch (iDigital) {
					case 0:
					
					case 1:
						strInput+= pStr;
						this.setText(strInput);
						break;
					case 2:
						//
						break;

					}
				} else {
					strInput+= pStr;
					this.setText(strInput);

				}
		}
	}
	
	protected void onZeroClick() {
		String strInput = mNumberPadInput.getText().toString().trim();
		if (strInput.length() < mMaxLength){
				if (strInput.contains(".")) {
					int positionOfDot = strInput.indexOf(".");
					int iDigital = 0;
					iDigital = strInput.substring(positionOfDot).length() - 1;
					switch (iDigital) {
					case 0:
					
					case 1:
						onNumberClick("0");
						break;
					case 2:
						//
						break;

					}
				} else {
					onNumberClick("0");

				}
		}
		
	}

	protected void onDoubleZeroClick() {
		String strInput = mNumberPadInput.getText().toString().trim();
		if (strInput.length() < mMaxLength){
//			if (!strInput.equals("")) {
				if (strInput.contains(".")) {
					int positionOfDot = strInput.indexOf(".");
					int iDigital = 0;
					iDigital = strInput.substring(positionOfDot).length() - 1;
					switch (iDigital) {
					case 0:
						onNumberClick("00");
						break;
					case 1:
						onNumberClick("0");
						break;
					case 2:
						//
						break;

					}
				} else {
					onNumberClick("00");

				}
//			}
		}
	}

	protected void onDotClick() {
		String strInput = mNumberPadInput.getText().toString().trim();
		if(!strInput.contains(".")){
			onNumberClick(".");
		}
		
	}

	protected void onMultiplyClick() {
//		isMultiply = true;
		String strInput = mNumberPadInput.getText().toString().trim();
		if(strInput.equals("")) return;
		
		 if(TextUtils.isDigitsOnly(strInput)){
			 
			 int qty = Integer.valueOf(strInput);
			 if (qty >= 1){
				 this.iQty = qty;
			 }
			
		 }else{
			 this.iQty = 1;
		 }
		 
		 mNumberPadInput.setText("");
		
		
	}

	public void onClearClick() {
		 mNumberPadInput.setText("");
		
	}

	private String valueBuilder(String str, String str1) {
		// TODO Auto-generated method stub
		return null;
	}

	private String numBuilder(String str, String str1) {
		String strTmp = str1 + str;
		if (strTmp.length() < this.mMaxLength)
			return strTmp;
		else
			return "";

	}

	private NumberPadListener mNumberPadListener;

	boolean resetInput = false;

	private View mNumberPadView;
	private Button mButtonKeyDot;
	private LayoutParams LP_FF = new LayoutParams(LayoutParams.FILL_PARENT,
			LayoutParams.FILL_PARENT);
	private int iQty;

	private void constructor(Context context) {

		this.mContext = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		mNumberPadView = mInflater.inflate(R.layout.number_pad_for_payment, null);
		mNumberPadInput =  (EditText) mNumberPadView.findViewById(R.id.numberPadInput);
		mButtonKey_0 = (Button) mNumberPadView.findViewById(R.id.numberPadKey_0);
		mButtonKey00 = (Button) mNumberPadView.findViewById(R.id.numberPadKey00); 
		mButtonKey01 = (Button) mNumberPadView.findViewById(R.id.numberPadKey01);
		mButtonKey02 = (Button) mNumberPadView.findViewById(R.id.numberPadKey02);
		mButtonKey03 = (Button) mNumberPadView.findViewById(R.id.numberPadKey03); 
		mButtonKey04 = (Button) mNumberPadView.findViewById(R.id.numberPadKey04); 
		mButtonKey05 = (Button) mNumberPadView.findViewById(R.id.numberPadKey05); 
		mButtonKey06 = (Button) mNumberPadView.findViewById(R.id.numberPadKey06);
		mButtonKey07 = (Button) mNumberPadView.findViewById(R.id.numberPadKey07); 
		mButtonKey08 = (Button) mNumberPadView.findViewById(R.id.numberPadKey08); 
		mButtonKey09 = (Button) mNumberPadView.findViewById(R.id.numberPadKey09); 
//		mButtonKeyX = (Button) mNumberPadView.findViewById(R.id.numberPadKeyX);
		mButtonKeyClear = (Button) mNumberPadView.findViewById(R.id.numberPadKeyClear);
		mButtonKeyBack = (Button) mNumberPadView.findViewById(R.id.numberPadKeyBack);
		mButtonKeyEnter = (Button) mNumberPadView.findViewById(R.id.numberPadKeyEnter);
		mButtonKeyDot = (Button) mNumberPadView.findViewById(R.id.numberPadKeyDot);
		
		mButtonKey_0.setTag(NumberPadButton.ZERO);
		mButtonKey00.setTag(NumberPadButton.DOUBLEZERO);
		mButtonKey01.setTag(NumberPadButton.ONE);
		mButtonKey02.setTag(NumberPadButton.TWO);
		mButtonKey03.setTag(NumberPadButton.THREE);
		mButtonKey04.setTag(NumberPadButton.FOUR); 
		mButtonKey05.setTag(NumberPadButton.FIVE); 
		mButtonKey06.setTag(NumberPadButton.SIX);
		mButtonKey07.setTag(NumberPadButton.SEVEN); 
		mButtonKey08.setTag(NumberPadButton.EIGHT); 
		mButtonKey09.setTag(NumberPadButton.NINE);
//		mButtonKeyX.setTag(NumberPadButton.MULTIPLY);
		mButtonKeyClear.setTag(NumberPadButton.CLEAR);
		mButtonKeyBack.setTag(NumberPadButton.BACKSPACE);
		mButtonKeyEnter.setTag(NumberPadButton.ENTER);
		mButtonKeyDot.setTag(NumberPadButton.DOT);
		
		mButtonKey_0.setOnClickListener(mOnButtonClick2);
		mButtonKey00.setOnClickListener(mOnButtonClick2);
		mButtonKey01.setOnClickListener(mOnButtonClick2);
		mButtonKey02.setOnClickListener(mOnButtonClick2);
		mButtonKey03.setOnClickListener(mOnButtonClick2);
		mButtonKey04.setOnClickListener(mOnButtonClick2); 
		mButtonKey05.setOnClickListener(mOnButtonClick2); 
		mButtonKey06.setOnClickListener(mOnButtonClick2);
		mButtonKey07.setOnClickListener(mOnButtonClick2); 
		mButtonKey08.setOnClickListener(mOnButtonClick2); 
		mButtonKey09.setOnClickListener(mOnButtonClick2);
//		mButtonKeyX.setOnClickListener(mOnButtonClick2);
		mButtonKeyClear.setOnClickListener(mOnButtonClick2);
		mButtonKeyBack.setOnClickListener(mOnButtonClick2);
		mButtonKeyEnter.setOnClickListener(mOnButtonClick2);
		mButtonKeyDot.setOnClickListener(mOnButtonClick2);
		
		
		mNumberPadInput.setOnTouchListener(editTextOnTouchEvent);
		
		mNumberPadView.setLayoutParams(LP_FF);
		
		this.setLayoutParams(LP_FF);
		this.addView(mNumberPadView);
		populateLayout();
		
		
		
		isReadOnly = false;
		isMultiply = false;
		iQty = 1;
	}

	
	private void populateLayout() {

		

	}

	

	
	
	public void setFoucus(){
		mNumberPadInput.setSelected(true);
		mNumberPadInput.setFocusable(true);
		mNumberPadInput.setFocusableInTouchMode(true);
		mNumberPadInput.requestFocus();
	}

	public void setNumberPadListener(NumberPadListener listener) {

		this.mNumberPadListener = listener;

	}

	public void setStyle(int style) {
		switch (style) {
		case LOGIN_STYLE:
			this.mNumberPadInput.setVisibility(INVISIBLE);
//			this.mButtonKeyX.setVisibility(INVISIBLE);
			break;
		case SALE_STYLE:
			this.mButtonKeyDot.setText("");
			break;
		case CHECK_OUT_STYLE:
			//this.mNumberPadInput.setVisibility(INVISIBLE);
//			this.mButtonKeyX.setVisibility(INVISIBLE);
			break;
		case NO_ENTER_STYLE:
			this.mButtonKeyEnter.setText("");
//			this.mButtonKeyX.setVisibility(INVISIBLE);
			break;
		case NO_ENTER_DOT_STYLE:
			this.mButtonKeyEnter.setText("");
			this.mButtonKeyDot.setText("");
			this.mButtonKeyDot.setEnabled(false);
			break;
		default:
			//
			break;

		}

	}

	public interface NumberPadListener {

//		void onEnterClick();

		void onClick(NumberPadButton button);

//		void onMultiplyClick(int iQty);
		
//		void onSyncBuffer(String sInput);

	}

//	public void setBuffer(String str) {
//		this.mInputBuffer = str;
//		this.mNumberPadInput.setText(str);
//		
//	}
//	
	public String getBuffer(){
		return  mNumberPadInput.getText().toString().trim();
	}
	
	public int getQty(){
		return iQty;
	}
	

	public void setInputEditText(EditText et) {
		this.mNumberPadInput = et;
//		this.mNumberPadInput.setSelected(true);
//		this.mNumberPadInput.setFocusable(true);
//		this.mNumberPadInput.setFocusableInTouchMode(true);
//		this.mNumberPadInput.requestFocus();
//		this.mNumberPadInput.setInputType(InputType.TYPE_NULL);
		this.mNumberPadInput.setOnTouchListener(editTextOnTouchEvent);

	}

	public void reset() {
		this.mNumberPadInput.setInputType(InputType.TYPE_NULL);
		this.mNumberPadInput.setText("");
		this.iQty = 1;
		
	}

	public void setBuffer(String res) {
		//mNumberPadInput.setText("");
//		mNumberPadInput.setText(res);
		this.setText(res);
		
	}


	public void disableKey(int id) {
		Button btn= (Button) this.findViewById(id);
		btn.setEnabled(false);
		btn.setText("");	
	}
	
	

}
